package com.os.opencv.java.chapter6;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;

import java.util.Random;

public class Peppersalt {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        //读取图像文件并在屏幕上显示
        Mat img = Imgcodecs.imread("/Users/matt/Pictures/111.jpg");
        HighGui.imshow("src", img);
        HighGui.waitKey(0);

        int row = img.rows();
        int col = img.cols();
        //定义黑白两种颜色
        double[] black = new double[]{0,0,0};
        double[] white = new double[]{255,255,255};

        //用于获取随机数的random类
        Random random = new Random();

        for(int n=0; n<20000; n++){
            //随机获取行号、列号及噪点颜色
            int i = random.nextInt(row);
            int j = random.nextInt(col);
            boolean isBlack = random.nextBoolean();

            //在相应的像素上加上黑白噪点
            if(isBlack){
                img.put(i, j, black);
            }else{
                img.put(i, j, white);
            }
        }

        //在屏幕上显示加了椒盐噪点的图像
        HighGui.imshow("peppersalt", img);
        HighGui.waitKey(0);

        System.exit(0);

    }
}
